// pages/service/order-list/index.js
var api = require("../../../../api.js"),
  QQMapWX = require("../../../../qqmap-wx.js"),app = getApp();
// 播放器
const innerAudioContext = wx.createInnerAudioContext()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentItemId: "1",
    orderNumber: '',
    startTime: '',
    endTime: '',
    data: [],
    pageSize: 5,
    currentPageSize: 5,
    soundRecording: {
      tempPath: "",
      duration: "",
      isPlay: false
    }, // 录音
  },
  onLoad: function (options) {
    var that=this;
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
    })
    app.request({
      url: api.order.CheckOpenid,
     
      data:{
        uid:wx.getStorageSync('uid'),
        bid:wx.getStorageSync('bid')
      },
      success:function(res){
        if(res.code==2){
          that.setData({
            service_url:res.url
          })
        }
      }
    })
  },
  selectSwiper: function (e) {
    var that = this;

    var val = e.currentTarget.dataset.listid;
    that.initCurrentPageSize();
    that.setData({
      currentItemId: val
    });
    that.list();
  },
  changeSwiper: function (e) {

    var that = this;
    var val = e.detail.currentItemId;
    that.initCurrentPageSize();
    that.setData({
      currentItemId: val
    });
    that.list();

  },
  onShow: function () {

    /**订单列表 */
    var that = this
    this.userinfo();
    that.check();
    that.list();
    
  },
  list: function () {
    var that = this;
    console.log(that.data.currentPageSize);
    app.request({
      url: api.default.ServerOrder,
      data: {
        bid: wx.getStorageSync('bid'),
        status: that.data.currentItemId,
        uid: wx.getStorageSync('uid'),
        limit: that.data.currentPageSize
      },
      success: function (res) {
        console.log('服务订单', res)
        var data = []
        that.setData({
          data: res.data
        })

      }
    })
  },
  check: function () {
    if (wx.getStorageSync('phone') == '') {

      wx.redirectTo({
        url: '/sd_liferuning/pages/login/login',
      })
      return false;
    }
    if (wx.getStorageSync('cash').status == 1) {

      wx.redirectTo({
        url: '../authentication/index',
      })
      return false;
    }
    if (wx.getStorageSync('cash').status == 2) {

      wx.redirectTo({
        url: '../auth-success/index',
      })
      return false;
    }
    if (wx.getStorageSync('cash').status == -1) {

      wx.showModal({
        title: '提示',
        content: '您已被管理员封禁，请与管理员联系',
        showCancel: false,
        success: function () {
          wx.redirectTo({
            url: '../user/index',
          })
        }
      })
      return false;
    }

    if (wx.getStorageSync('cash').cashstatus == -1) {

      wx.redirectTo({
        url: '../auth-success/index',
      })
      return false;
    }
    if (wx.getStorageSync('cash').status == 3) {
      if (wx.getStorageSync('cash').cashstatus == 0) {


        wx.showModal({
          title: '提示',
          content: '您还未缴纳保证金',
          showCancel: false,
          success: function () {
            wx.redirectTo({
              url: '../userauth-pay/index',
            })
          }
        })
        return false;
      }
    }
  },
  userinfo: function () {
    var that = this;
    app.request({
      url: api.default.userinfos,
      data: {
        uid: wx.getStorageSync('uid')
      },
      success: function (e) {
        if (e.code == 1) {

          that.setData({
            info: e.data
          })
          if (e.data.status == -1) {
            wx.removeStorage({
              key: 'cash',
              success: function (res) {
                wx.showModal({
                  title: '提示',
                  content: '您已经被管理员封禁',
                  showCancel: false,
                  success: function () {
                    wx.redirectTo({
                      url: '../user/index',
                    })
                  }
                })
              },
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '获取用户信息失败',
          })
        }
      }
    })

  },
  navToUrl: function (e) {
    let _url = e.currentTarget.dataset.url;
    console.log(_url);
    wx.navigateTo({
      url: _url,
    })
  },
  /**抢单 */
  GiveOrder: function (e) {

    app.request({
      url: api.default.GiveOrder,
      data: {
        orderid: e.currentTarget.dataset.id,
        uid: wx.getStorageSync('uid'),
        bid: wx.getStorageSync('bid')
      },
      success: function (res) {
        console.log('订单详情', res.data)
        console.log(res)
        if (res.data == 1) {
          app.request({
            url: api.user.mess,
            data: ({
              bid: wx.getStorageSync('bid'),
              order_no: e.currentTarget.dataset.order_no,
              type: 'order'//接单消息类型
            }),
            success: function (rs) {
              console.log(rs)
            }
          })
          wx.showToast({
            title: '恭喜，抢单成功啦！',
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.redirectTo({
                  url: '/service/pages/service/index/index',
                })
              }, 1500)
            }
          })
        }else if(res.status==0){
          wx.showToast({
            title: '手慢啦,已被抢走！',
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.redirectTo({
                  url: '/service/pages/service/index/index',
                })
              }, 1500)
            }
          })
        } else if (res.status == -1){
          wx.showToast({
            title: '您已满单！',
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.redirectTo({
                  url: '/service/pages/service/index/index',
                })
              }, 1500)
            }
          })
        }

      }
    })
  },
  /**完成订单 */
  OkOrder: function (e) {

    app.request({
      url: api.default.OkOrder,
      data: {
        orderid: e.currentTarget.dataset.id,
        bid: wx.getStorageSync('bid'),
        uid: wx.getStorageSync('uid')
      },
      success: function (res) {
        console.log('订单详情', res.data)
        if (res.data == 1) {
          wx.showToast({
            title: '感谢您的付出，您将有丰厚的回报！',
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.redirectTo({
                  url: '/service/pages/service/index/index',
                })
              }, 1000)
              return false;
            }
          })

        }
      }
    })
  },
  /**
   * 数据刷新
   */
  refreshData: function () {
    let _this = this;
    let _currentItemId = _this.data.currentItemId;  // 当前选中的ID
    let _pageSize = _this.data.pageSize;  // 每次加载多少条
    let _currentPageSize = _this.data.currentPageSize;  // 当前数据
    _currentPageSize += _pageSize;
    _this.setData({
      currentPageSize: _currentPageSize
    });

    _this.sendRequestByCurrentPageSize({ currentItemId: _currentItemId, currentPageSize: _currentPageSize });
  },
  /**
   * 执行查询
   * @param currentItemId 当前查询的ID
   * @param currentPageSize 当前页面需要查询的总数
   */
  sendRequestByCurrentPageSize({ currentItemId, currentPageSize }) {
    var that = this;
    let _currentItemId = currentItemId;
    let _currentPageSize = currentPageSize;
    // TODO 执行查询
  
   that.list();
  

  },
  /**
   * 初始化当页数据
   */
  initCurrentPageSize: function () {
    let _this = this;
    let _pageSize = _this.data.pageSize;  // 每次加载多少条
    _this.setData({
      currentPageSize: _pageSize
    });
  },
  // 预览图片
  previewPic: function (e) {
    let _value = e.currentTarget.dataset.value;
    wx.previewImage({
      current: _value.xphoto, // 当前显示图片的http链接
      urls: [_value.xphoto] // 需要预览的图片http链接列表
    })
  },
  /**
   * 录音播放
   */
  soundRecordingPlay: function (e) {
    let _value = e.currentTarget.dataset.value;
    let _this = this;
    innerAudioContext.src = _value.yinpin;
    let _paused = innerAudioContext.paused;
    let _soundRecording = _this.data.soundRecording;
    if (_paused) {
      innerAudioContext.play();
      _soundRecording.isPlay = true;
      setTimeout(function () {
        let _soundRecording = _this.data.soundRecording;
        _soundRecording.isPlay = false;
        _this.setData({
          soundRecording: _soundRecording
        });
      }, _soundRecording.duration * 1000)
    } else {
      innerAudioContext.stop();
      _soundRecording.isPlay = false;
    }
    _this.setData({
      soundRecording: _soundRecording
    });
  },
})